﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameJamKingdom
{
    class GameplayState : State
    {
        Arena arena;
        public SpriteFont font;
        bool fLeft, fUp, fRight, fDown;

        Texture2D arrowPtr;
        bool arrowVis;

        public int score; 

        public GameplayState(Game1 cGame)
        {
            Initialize(cGame);
        }

        public override void Initialize(Game1 cGame)
        {
            arena = new Arena(cGame);
            font = cGame.Content.Load<SpriteFont>("ScoreFont");

            arrowPtr = cGame.Content.Load<Texture2D>("Arrow");
            arrowVis = true;

            fLeft = false;
            fUp = false;
            fRight = false;
            fDown = false;
        }

        public override void Update(Game1 cGame, GameTime gameTime)
        {
            arena.tick(cGame, gameTime);

            if (arena.getBlock().type.Equals("lava"))
            {
                cGame.sbank.play("death", 0.1f);
                cGame.cState = "gameover";
                cGame.finalScore = score;
                score = 0;
            }
            if (arena.getBlock().type.Equals("coin"))
            {
                cGame.sbank.play("goldScale", 0.1f);
                score = score + (75 - arena.scrollSpeed);
                arena.setBlock("empty");
            }
            if (arena.getBlock().type.Equals("hammer"))
            {
                cGame.sbank.play("powerup", 0.1f);
                arena.character.cMode = 1;
                arena.setBlock("empty");
            }
            if (arena.getBlock().type.Equals("flipper"))
            {
                cGame.sbank.play("powerup", 0.1f);
                arena.character.cMode = 2;
                arena.setBlock("empty");
            }
            if (arena.getBlock().type.Equals("brick"))
            {
                cGame.sbank.play("brickWall", 0.1f);
            }
            if (arena.getBlock().type.Equals("water"))
            {                
                cGame.sbank.play("swim", 0.01f);
            }
            if (arena.getBlock().type.Equals("time"))
            {
                arena.setBlock("empty");
                arena.sTime = arena.pTime;
                cGame.sbank.play("time", 0.01f);
            }
            if (arena.getBlock().type.Equals("ice"))
            {
                cGame.sbank.play("ice", 0.01f);
                if (arena.character.lastDirection.Equals("right"))
                    arena.character.gridPos.X++;
                else if (arena.character.lastDirection.Equals("left"))
                    arena.character.gridPos.X--;
                else if (arena.character.lastDirection.Equals("up"))
                    arena.character.gridPos.Y--;
                else
                    arena.character.gridPos.Y++;

                if ((int)arena.character.gridPos.X > Program.numColumns - 1)
                    arena.character.gridPos.X = Program.numColumns - 1;

                if ((int)arena.character.gridPos.X < 0)
                    arena.character.gridPos.X = 0;

                if ((int)arena.character.gridPos.Y < 0)
                    arena.character.gridPos.Y = 0;

                if ((int)arena.character.gridPos.Y > Program.numRows - 1)
                    arena.character.gridPos.Y = Program.numRows - 1;

            }
            if (arena.getBlock().type.Equals("spring"))
            {
                arena.character.lastDirection = "right";
                cGame.sbank.play("spring", 0.01f);
                int width = arena.rand.Next(3) - 1;
                int length = arena.rand.Next(3) + 2;
                arena.character.gridPos.X = arena.character.gridPos.X + length;
                arena.character.gridPos.Y = arena.character.gridPos.Y + width;

                if ((int)arena.character.gridPos.X > Program.numColumns - 1)
                    arena.character.gridPos.X = Program.numColumns - 1;

                if ((int)arena.character.gridPos.Y < 0)
                    arena.character.gridPos.Y = 0;

                if ((int)arena.character.gridPos.Y > Program.numRows - 1)
                    arena.character.gridPos.Y = Program.numRows - 1;

                while (arena.getBlock().type.Equals("lava"))
                {
                    if ((int)arena.character.gridPos.X + 1 > Program.numColumns - 1)
                    {
                        arena.character.gridPos.X = Program.numColumns - 1;
                        break;
                    }
                    else
                    {
                        arena.character.gridPos.X = arena.character.gridPos.X + 1;
                    }
                }
            }

            KeyboardState cKeyState;
            cKeyState = Keyboard.GetState();

            arena.character.timeLastAction = arena.character.timeLastAction + gameTime.ElapsedGameTime.Milliseconds;
            //score = gameTime.ElapsedGameTime.Milliseconds;

            if (cKeyState.IsKeyDown(Keys.Right))
            {
                fRight = true;
            }
            else if (cKeyState.IsKeyDown(Keys.Left))
            {
                fLeft = true;
            }
            else if (cKeyState.IsKeyDown(Keys.Up))
            {
                fUp = true;
            }
            else if (cKeyState.IsKeyDown(Keys.Down))
            {
                fDown = true;
            }

            if (cKeyState.IsKeyUp(Keys.Right) && fRight)
            {
                arena.move("right", gameTime.ElapsedGameTime.Milliseconds);
                fRight = false;
            }
            else if (cKeyState.IsKeyUp(Keys.Left) && fLeft)
            {
                arena.move("left", gameTime.ElapsedGameTime.Milliseconds);
                fLeft = false;
            }
            else if (cKeyState.IsKeyUp(Keys.Up) && fUp)
            {
                arena.move("up", gameTime.ElapsedGameTime.Milliseconds);
                fUp = false;
            }
            else if (cKeyState.IsKeyUp(Keys.Down) && fDown)
            {
                arena.move("down", gameTime.ElapsedGameTime.Milliseconds);
                fDown = false;
            }

            if (cKeyState.IsKeyDown(Keys.Escape))
            {
                cGame.cState = "menu";
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            arena.render(spriteBatch);

            if (arrowVis)
                spriteBatch.Draw(arrowPtr, new Rectangle(200- arena.offset, 225,100,100), Color.White);

            string message = "Score: " + score.ToString();
            spriteBatch.DrawString(font, message, new Vector2(Program.wWidth / 2 - font.MeasureString(message).X / 2, 560), Color.White);
            spriteBatch.End();
            
        }
    }
}
